package qwqw;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DefaultCaret;

/**
 * The main Frame that holds the server GUI.
 * The console is directed to the output stream text area.
 */
public class ServerFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	final public static int DEFAULT_PORT = 2222;
	private String myIP=null;
	EchoServer sv = null;
	mysqlConnection db = null;
	
	private JPanel firstPanel = null;
	private JLabel mainLabel = null;
	private JLabel portLabel = null;
	private JTextField portTextField = null;
	private JLabel ipLabel = null;
	private JLabel ip2Label = null;
	private JButton startButton = null;
	private JButton stopButton = null;
	private JTextArea ta1 = null;
	private JButton exitButton = null;
	private JScrollPane jScrollPane = null;
	
	/**
	* Constructs an instance of the ServerFrame GUI.
	* It initializes the screen and the first panel.
	*/	
	public ServerFrame(String ip){
		
		super("ICM - Server");
		this.myIP = ip;
		setPreferredSize(new Dimension(650,550));
		setLocation(700, 0);
		add(getFirstPanel());
		setContentPane(firstPanel);
		setVisible(true);
		
		
		initialize();
		
		setActionEvents();
	}
		
				
	/**
	 * This method initializes ta1	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getTa1() {
		if (ta1 == null) {
			ta1 = new JTextArea();
			DefaultCaret caret = (DefaultCaret)ta1.getCaret();
			caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
			TextAreaOutputStream taos = new TextAreaOutputStream( ta1, 60 );
			PrintStream ps = new PrintStream( taos );
			System.setOut( ps );
			System.setErr( ps );
		}
		
		return ta1;
	}


/**
 * This method initializes exitButton	
 * 	
 * @return javax.swing.JButton	
 */
private JButton getExitButton() {
	if (exitButton == null) {
		exitButton = new JButton();
		exitButton.setBounds(new Rectangle(214, 99, 98, 26));
		exitButton.setText("EXIT");
	}
	return exitButton;
}


/**
 * This method initializes jScrollPane	
 * 	
 * @return javax.swing.JScrollPane	
 */
private JScrollPane getJScrollPane() {
	if (jScrollPane == null) {
		jScrollPane = new JScrollPane();
		jScrollPane.setBounds(new Rectangle(15, 140, 516, 313));
		jScrollPane.setViewportView(getTa1());
	}
	return jScrollPane;
}


public static void main(String[] args) {
		
	String ip=null;
	try {
			ip=(InetAddress.getLocalHost().getHostAddress());
			System.out.println("IP: "+ip);
			} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new ServerFrame(ip);
		
	}
	
	private void initialize() {
        this.setSize(new Dimension(563, 503));
        this.setContentPane(getFirstPanel());
	}

	/**
	 * This method initializes stopButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getStopButton() {
		if (stopButton == null) {
			stopButton = new JButton();
			stopButton.setBounds(new Rectangle(112, 98, 94, 27));
			stopButton.setText("STOP");
		}
		return stopButton;
	}




	/**
	 * This method initializes this
	 * 
	 */
	
	

	/**
	 * This method initializes firstPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getFirstPanel() {
		if (firstPanel == null) {
			ip2Label = new JLabel();
			ip2Label.setBounds(new Rectangle(212, 61, 128, 24));
			ip2Label.setText(this.myIP);
			ipLabel = new JLabel();
			ipLabel.setBounds(new Rectangle(144, 59, 59, 27));
			ipLabel.setText("Server IP:");
			portLabel = new JLabel();
			portLabel.setBounds(new Rectangle(14, 60, 38, 25));
			portLabel.setText("Port:");
			mainLabel = new JLabel();
			mainLabel.setBounds(new Rectangle(46, 12, 209, 34));
			mainLabel.setText("ICM SERVER CONFIGURATION");
			firstPanel = new JPanel();
			firstPanel.setLayout(null);
			
			firstPanel.add(mainLabel, null);
			firstPanel.add(portLabel, null);
			firstPanel.add(getPortTextField(), null);
			firstPanel.add(ipLabel, null);
			firstPanel.add(ip2Label, null);
			firstPanel.add(getStartButton(), null);
			firstPanel.add(getStopButton(), null);
			firstPanel.add(getExitButton(), null);
			firstPanel.add(getJScrollPane(), null);
		}
		return firstPanel;
	}
	
	private JTextField getPortTextField() {
		if (portTextField == null) {
			portTextField = new JTextField();
			portTextField.setBounds(new Rectangle(60, 60, 73, 24));
			portTextField.setText("5555");
		}
		return portTextField;
	}

	private JButton getStartButton() {
		if (startButton == null) {
			startButton = new JButton();
			startButton.setBounds(new Rectangle(13, 97, 89, 29));
			startButton.setText("START");
		}
		return startButton;
	}
	
	/**
	* Set the action events for all of the buttons in the frame.
	*/
	public void setActionEvents()
	{
		
		this.getStartButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				
				if (sv != null){
					if (sv.isListening()){
						JOptionPane.showMessageDialog(null,"Server is already listening!","Attention",JOptionPane.INFORMATION_MESSAGE);
						return;
					}
				}
				int port;
				try
				 {
				   port = Integer.parseInt(getPortTextField().getText()); //Get port from command line
				 }
				 catch(Throwable t)
				 {
				   port = DEFAULT_PORT; //Set port to 5555
				 }
				
				if ((port <1025)||(port > 65535)){
					JOptionPane.showMessageDialog(null,"Please choose port in range 1025-65535!","Attention",JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				
				 sv = new EchoServer(port);
				 
				 
				 try 
				 {
				   sv.listen(); //Start listening for connections
				   
				 } 
				 catch (Exception ex) 
				 {
				   System.out.println("ERROR - Could not listen for clients!");
				 }
				
			
			}
		});
	
		
		this.getExitButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				
				if (sv == null){
					System.exit(0);
				}
					
				try {
					sv.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.exit(0);
			}
		});
		this.getStopButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				
				if (sv == null)
					return;
				try {
					
					sv.close();
					sv = null;
					
					System.out.println("SERVER STOPPED - Press start to reset");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		
		
		this.addWindowListener(new WindowListener() {
			public void windowClosing(WindowEvent evt) 
			{
				try {
					sv.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.exit(0);
			}
	
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
} 
